//
//  ViewController.m
//  JXIntercomDemo
//
//  Created by Nansen on 2020/3/23.
//  Copyright © 2020 jingxi. All rights reserved.
//

#import "ViewController.h"
#import <JXIntercomSDK/JXIntercomSDK.h>
#import "JXListViewController.h"
#import "JXVideoViewController.h"
#import "JX_DemoConfig.h"

@import AVFoundation;


@interface ViewController ()<JXIntercomDelegate>

@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@property (weak, nonatomic) IBOutlet UITextField *userTF;
@property (weak, nonatomic) IBOutlet UITextField *aliasTF;
@property (weak, nonatomic) IBOutlet UITextField *buttonKeyTF;

@property (weak, nonatomic) IBOutlet UITextField *appIdTF;
@property (weak, nonatomic) IBOutlet UITextField *appKeyTF;
@property (weak, nonatomic) IBOutlet UITextField *activeCodeTF;


@property (nonatomic, assign) BOOL isLogin;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.isLogin = NO;
    
    self.versionLabel.text = [NSString stringWithFormat:@"SDK Version: %@", [JXManager version]];
    
    self.userTF.text = [JX_DemoConfig userId];
    self.aliasTF.text = [JX_DemoConfig alias];
    
    self.appIdTF.text = [JX_DemoConfig appid];
    self.appKeyTF.text = [JX_DemoConfig appkey];
    self.activeCodeTF.text = [JX_DemoConfig activeCode];
    
    [JXManager defaultManage].appDelegate = self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



- (IBAction)loginAction:(UIButton *)sender {
    
    if (self.isLogin == NO) {
        NSString *userId = self.userTF.text;
        NSString *alias = self.aliasTF.text;
        NSString *buttonKey = self.buttonKeyTF.text;
        
        BOOL result = [[JXManager defaultManage] loginWithUserId:userId alias:alias buttonKey:buttonKey];
        if (result) {
            NSLog(@"激活 SDK");
            
            NSString *appId = self.appIdTF.text;
            NSString *appKey = self.appKeyTF.text;
            NSString *activeCode = self.activeCodeTF.text;
            
            if (appId && appId.length > 0 &&
                appKey && appKey.length > 0 &&
                activeCode && activeCode.length > 0) {
                [[JXManager defaultManage] checkActiveWithAppId:appId appKey:appKey activeCode:activeCode];
            }
            
            
            JXListViewController *listVC = [[JXListViewController alloc] init];
            [self.navigationController pushViewController:listVC animated:YES];

            self.isLogin = YES;
            sender.selected = YES;
        }
        else {
            NSLog(@"SDK 配置出错!");
        }
    }
    else {
        
        [[JXManager defaultManage] logout];
        
        self.isLogin = NO;
        sender.selected = NO;
    }
}

#pragma mark - ======== JXIntercomDelegate ========
/// 有呼叫进来, 是否需要响应.
- (BOOL)shouldResponseIntercomCall:(JX_IntercomScenes)callScenes
{
    UIViewController *vc = self.navigationController.topViewController;
    if (vc.presentedViewController) {
        return NO;
    }
    return YES;
}


/// 被门禁呼叫 (shouldResponseIntercomCall 返回 YES 的时候才会有)
- (id<JXConnectingDelegate>)calledByDoorWithHomeId:(NSString *)homeId
                                         sessionId:(NSString *)sessionId
                                        doorDevice:(JXDoorDeviceModel *)doorDevice
{
    JXVideoViewController *vc = [[JXVideoViewController alloc] initWithCallDevice:doorDevice callType:JX_IntercomCallType_Monitor callScenes:JX_IntercomScenes_Door isCallout:NO homeId:homeId];
    vc.sessionId = sessionId;
    if (@available(iOS 13.0, *)) {
        vc.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    [self presentViewController:vc animated:YES completion:nil];
    return vc;
}

/// 被室内通呼叫 (shouldResponseIntercomCall 返回 YES 的时候才会有)
- (id<JXConnectingDelegate>)calledByExtWithHomeId:(NSString *)homeId
                                        sessionId:(NSString *)sessionId
                                        extDevice:(JXExtDeviceModel *)extDevice
{
    JXVideoViewController *vc = [[JXVideoViewController alloc] initWithExtDevice:extDevice callType:JX_IntercomCallType_Call callScenes:JX_IntercomScenes_Ext isCallout:NO homeId:homeId];
    vc.sessionId = sessionId;
    if (@available(iOS 13.0, *)) {
        vc.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    [self presentViewController:vc animated:YES completion:nil];
    return vc;
}

- (id<JXConnectingDelegate>)calledByP2PWithHomeId:(NSString *)homeId sessionId:(NSString *)sessionId callNumber:(NSString *)callNumber
{
    JXVideoViewController *vc = [[JXVideoViewController alloc] initWithCallP2P:callNumber homeId:homeId isCallout:NO];
    vc.sessionId = sessionId;
    if (@available(iOS 13.0, *)) {
        vc.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    [self presentViewController:vc animated:YES completion:nil];
    return vc;
}


- (void)onDeviceActivated:(DeviceActiveResult)result message:(NSString *)message
{
    if (result == DeviceActive_Success) {
        NSLog(@"鉴权-成功");
    }
    else {
        NSString *errorMessage = @"激活异常";
        switch (result) {
            case DeviceActive_ErrorLib:
                errorMessage = @"SDK库有误";
                break;
            case DeviceActive_ErrorNet:
                errorMessage = @"网络不通";
                break;
            case DeviceActive_CodeUsedInOtherDevice:
                errorMessage = @"当前提供的激活码已经在其他设备上激活";
                break;
            case DeviceActive_Expired:
                errorMessage = @"激活码已经过期";
                break;
            case DeviceActive_Invalid:
                errorMessage = @"激活码无效";
                break;
            case DeviceActive_AlreadyActivated:
                errorMessage = @"该设备已经使用其他激活码激活了";
                break;
            default:
                break;
        }
        NSLog(@"鉴权-失败:%@", errorMessage);
    }
}



@end
